﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
using FlexiCommerce.Components;
using Telerik.Web.UI;

namespace FlexiCommerce.Web.DynamicData.FieldTemplates
{
    public partial class Hierarchical_Parent_EditField : System.Web.DynamicData.FieldTemplateUserControl
    {
        private Category parent = null;
        private Category current = null;
        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
            parent = FieldValue as Category;
            object d = this.Page.GetDataItem();
            current = Row as Category;
            if (parent != null)
            {
                ParentTitle.Text = parent.Name;
            }
            else
            {
                ParentTitle.Text = "None (ROOT)";
            }


            if (current != null)
            {

            }


        }

        protected override void ExtractValues(System.Collections.Specialized.IOrderedDictionary dictionary)
        {
            if (RadTreeView1.SelectedNode != null)
            {
                dictionary[Column.Name] = Find.Categories.Where(c => c.ID == int.Parse(RadTreeView1.SelectedNode.Value)).SingleOrDefault();
            }
            else
            {
                dictionary[Column.Name] = null;
            }
        }

        protected void RadTreeView1_DataBound(object sender, EventArgs e)
        {
            if (parent != null)
            {
                RadTreeView1.FindNodeByValue(parent.ID.ToString()).Selected = true;
                RadTreeView1.FindNodeByValue(parent.ID.ToString()).ExpandParentNodes();
            }
            if (current != null)
            {
                RadTreeNode node = RadTreeView1.FindNodeByValue(current.ID.ToString());
                node.CssClass = "NotAllowedParent";
                foreach (Category category in Find.EnumerateChildren(current as IHierarchicalPersistenceItem))
                {
                    RadTreeNode childnode = RadTreeView1.FindNodeByValue(category.ID.ToString());
                    childnode.CssClass = "NotAllowedParent";
                }

            }
            if (HttpContext.Current.Request["parentID"] != null && Host.Mode == DataBoundControlMode.Insert)
            {
                RadTreeNode node = RadTreeView1.FindNodeByValue(HttpContext.Current.Request["parentID"]);
                if (node != null)
                {
                    node.Selected = true;
                    node.ExpandParentNodes();
                    ParentTitle.Text = node.Text;
                }
            }
        }
    }
}
